const { kuwo_request } = require("../../../util/kuwo_request");
const APIError = require("../../../middlewares/rest").APIError;


let song = async (ctx) => {

    if (ctx.request.method === 'GET') {
        var rid = ctx.request.query.rid || '156483846';
    } else if (ctx.request.method === 'POST') {
        var rid = ctx.request.body.rid || '156483846';
    }


    let result = await kuwo_request(`http://m.kuwo.cn/newh5app/api/mobile/v2/music/src/${rid}`)
    // console.log(result);
    // 捕获序列化json出错，防止程序异常退出
    if (result.data === 'failed') {
        throw new APIError("Song:url_notfound", "Song url is not found")
    }
    
    ctx.rest(result.data);
}


let songInfo = async (ctx) => {

    if (ctx.request.method === 'GET') {
        var rid = ctx.request.query.rid || '156483846';
    } else if (ctx.request.method === 'POST') {
        var rid = ctx.request.body.rid || '156483846';
    }

    let result = await kuwo_request('http://kuwo.cn/api/www/music/musicInfo', {
        mid: rid.trim(),
        httpsStatus: 1,
        reqId: 'e3f36a20-4c05-11eb-b0b7-8b03aa7e4b0d'
    });

    ctx.rest(result.data);
}

module.exports = {
    song,
    songInfo
}